/**
 * 
 */
package com.hawkfly.interceptor;

import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.intercept.AbsInterceptorDefaultAdapter;

/**
 * 用户角色关联时根据当前登录角色的不同对用户管理的角色进行筛选
 * @author hawkfly
 * @project sdcm
 */
public class ItprSdcmSelectRolesFilter extends AbsInterceptorDefaultAdapter
{
    @Override
	public Object beforeLoadData(ReportRequest rrequest,ReportBean rbean,Object typeObj,String sql)
    {
        String rolename = Wbcs4JBSFUtil.getRolenames(rrequest.getRequest().getSession().getAttribute(com.wbcs.jbsf.util.Consts.SESSION_ROLE));
        Object userid = rrequest.getRequest().getSession().getAttribute(com.wbcs.jbsf.util.Consts.SESSION_USERID);
        if(rolename.indexOf("编审") != -1){//具有编审身份则过滤角色数据
            StringBuffer tmpsqlBuf = new StringBuffer();
            StringBuffer appendConditions = new StringBuffer(" and (ID in(select id from jbsf_roles where isghost = 0 start with createrolecode = ")
                                            .append(userid).append(" connect by prior id = createrolecode").append(")")//保证挂起的幽灵角色可以在编审权限下使用
                                            .append(" or NAME in('记者','编辑','编审'))");
            if(sql.startsWith("select count(*) from (")){//数据统计语句
                tmpsqlBuf.append("select count(*) from (");
                String innersql = sql.substring(tmpsqlBuf.length(), sql.indexOf(") )  wx_tabletemp"));
                tmpsqlBuf.append(innersql).append(appendConditions).append(") )  wx_tabletemp");
                sql = tmpsqlBuf.toString();
            }else if(sql.startsWith("select * from (")){//数据查询语句
                tmpsqlBuf.append("SELECT * FROM(");
                String innersql = sql.substring(tmpsqlBuf.length()+1, sql.indexOf(") wbcs_temp_tbl"));
                tmpsqlBuf.append(innersql).append(appendConditions).append(") wbcs_temp_tbl");
                sql = tmpsqlBuf.toString();
            }
        }
        return sql;
    }
}